package cloud.daodao.storm.user.service;

import cloud.daodao.storm.common.security.model.captcha.CaptchaParam;
import cloud.daodao.storm.common.error.AppException;
import cloud.daodao.storm.common.user.error.UserError;
import cloud.daodao.storm.common.user.model.oauth2.IdLogin;
import cloud.daodao.storm.common.user.model.oauth2.Login;
import cloud.daodao.storm.common.user.model.oauth2.OAuth2;
import cloud.daodao.storm.common.util.security.MaskUtil;
import cloud.daodao.storm.user.helper.DictHelper;
import cloud.daodao.storm.user.helper.OAuth2Helper;
import cloud.daodao.storm.user.model.User;
import cloud.daodao.storm.user.repository.UserRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Slf4j
@Service
@Transactional
public class OAuth2Service {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private OAuth2Helper oAuth2Helper;

    @Resource
    private UserRepository userRepository;

    @Resource
    private DictHelper dictHelper;

    public OAuth2 login(Login param) {
        String captchaId = param.getCaptchaId();
        String captchaCode = param.getCaptchaCode();
        CaptchaParam captchaParam = new CaptchaParam();
        captchaParam.setCaptchaId(captchaId);
        captchaParam.setCaptchaCode(captchaCode);
        dictHelper.captchaCheck(captchaParam);

        String username = param.getUsername();
        String password = param.getPassword();
        User user = userRepository.findByUsername(username);
        if (null == user) {
            throw new AppException(UserError.USER_NOT_EXIST, username);
        }
        if (Boolean.FALSE.equals(user.getValid())) {
            throw new AppException(UserError.USER_INVALID, user.toJson());
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new AppException(UserError.USER_PASSWORD_INCORRECT, MaskUtil.mask(MaskUtil.MaskType.PASSWORD, password));
        }
        OAuth2 oAuth2 = oAuth2Helper.password(username, password);

        return oAuth2;
    }

    public OAuth2 idLogin(IdLogin param) {
        Long id = param.getId();
        Optional<User> optional = userRepository.findById(id);
        if (optional.isEmpty()) {
            throw new AppException(UserError.USER_NOT_EXIST, String.valueOf(id));
        }
        User user = optional.get();
        if (Boolean.FALSE.equals(user.getValid())) {
            throw new AppException(UserError.USER_INVALID, user.toJson());
        }
        return oAuth2Helper.id(id);
    }

    public void logout(String authorization) {
        // String authorization = requestHelper.header(HttpHeaders.AUTHORIZATION);
        if (null != authorization && !authorization.isEmpty()) {
            String token = authorization.replace("Bearer ", "");
            oAuth2Helper.revoke(token);
        }
    }

}
